/*
 * Copyright (C) 2015 Daniel Uppenkamp
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.maurersmarbles.drawables;

import com.google.common.base.Preconditions;
import com.googlecode.maurersmarbles.Card;
import com.googlecode.maurersmarbles.Orientation;
import com.googlecode.maurersmarbles.RGB;
import com.googlecode.maurersmarbles.Rectangle;
import com.googlecode.maurersmarbles.ScreenCanvas;

/**
 *
 * @author Daniel Uppenkamp
 */
public class CardDrawable implements Drawable {

    /**
     * The color of the selected rectangle.
     */
    protected static final int SELECTED_RECTANGLE_COLOR = RGB.YELLOW;
    /**
     * The size offset of the selected rectangle.
     */
    protected static final int SELECTED_RECTANGLE_OFFSET = 3;
    /**
     * The size offset of the selected rectangle.
     */
    protected static final int SELECTED_RECTANGLE_OFFSET_2X = SELECTED_RECTANGLE_OFFSET * 2;
    /**
     * Whether or not the card is selected.
     */
    protected boolean selected = false;
    /**
     * The card to draw.
     */
    protected Card card;
    /**
     * The orientation of the card.
     */
    protected Orientation orientation = Orientation.NORTH;
    /**
     * The rectangle to draw the card in.
     */
    protected Rectangle rectangle;

    /**
     * Creates a new CardDrawable.
     *
     * @param card the card
     * @param rectangle the rectangle to draw the card in
     */
    public CardDrawable(Card card, Rectangle rectangle) {
        this(card, rectangle, false);
    }

    /**
     * Creates a new CardDrawable.
     *
     * @param card the card
     * @param rectangle the rectangle to draw the card in
     * @param selected whether or not the card is selected
     */
    public CardDrawable(Card card, Rectangle rectangle, boolean selected) {
        
        Preconditions.checkNotNull(rectangle, "Null rectangle");
        
        this.card = card;        
        this.rectangle = rectangle;
        this.selected = selected;
        
    }

    /**
     * Sets the orientation of the cards display.
     *
     * @param orientation the orientation of the cards display
     */
    public void setOrientation(Orientation orientation) {
        Preconditions.checkNotNull(orientation, "Null orientation");
        this.orientation = orientation;
    }

    /**
     * Returns the rectangle to draw the card in.
     *
     * @return the rectangle to draw the card in
     */
    public Rectangle getRectangle() {
        return rectangle;
    }

    /**
     * Sets whether or not the card is selected.
     *
     * @param selected whether or not the card is selected
     */
    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    @Override
    public void draw(ScreenCanvas screenCanvas) {

        Preconditions.checkNotNull(screenCanvas, "Null screen canvas");
        
        // check to see if the card is face up or face down
        if (card != null) {

            // the card is face up, check to see if it is selected
            if (selected) {
                screenCanvas.fillRect(rectangle.getX() - SELECTED_RECTANGLE_OFFSET, rectangle.getY() - SELECTED_RECTANGLE_OFFSET, rectangle.getWidth() + SELECTED_RECTANGLE_OFFSET_2X, rectangle.getHeight() + SELECTED_RECTANGLE_OFFSET_2X, SELECTED_RECTANGLE_COLOR);
            }

            // TODO Update to draw card image
            screenCanvas.fillRect(rectangle, RGB.WHITE);
            screenCanvas.drawRect(rectangle, RGB.BLACK);
            screenCanvas.drawStringCentered(card.toString(), rectangle, RGB.BLACK);

        }
        else {
            // the card is face down
            screenCanvas.fillRect(rectangle.getX(), rectangle.getY(), rectangle.getWidth() + 1, rectangle.getHeight() + 1, RGB.MAGENTA);
        }

    }

}
